<template>
  <el-dialog v-model="drawer" @close="close" :append-to-body="true" width="600px" title="工作经历">
    <el-form :model="formData" class="refForm" size="small" ref="ruleRefForm" label-width="100px">
      <el-row>
        <!-- :prop="`list.${index}.company`" -->
        <el-col :span="12">
          <el-form-item label="工作单位" prop="companyName" :rules="{ required: true, message: '请输入工作单位', trigger: 'blur' }">
            <el-input placeholder="请输入" v-model="formData.companyName"></el-input>
          </el-form-item>
          <el-form-item label="工作开始时间" prop="startTime">
            <el-date-picker style="width:160px" v-model="formData.startTime" type="date" placeholder="请选择" size="small"/>
          </el-form-item>
          <el-form-item label="离职原因" prop="leaveReason">
            <el-input v-model="formData.leaveReason" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="证明人电话" prop="proverPhone">
            <el-input v-model="formData.proverPhone" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职务" prop="postName">
            <el-input placeholder="请输入" v-model="formData.postName"></el-input>
          </el-form-item>
          <el-form-item label="工作结束时间" prop="endTime">
            <el-date-picker style="width:160px" v-model="formData.endTime" type="date" placeholder="请选择" size="small"/>
          </el-form-item>
          <el-form-item label="证明人" prop="prover">
            <el-input v-model="formData.prover" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="工作备注" prop="remark">
            <el-input v-model="formData.remark" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-divider v-if="index < form.list.length - 1" style="margin: 0 0 18px" content-position="right">-->
      <!--        <el-button size="small" plain type="primary" link @click="delForm(index)">删除</el-button>-->
      <!--      </el-divider>-->
    </el-form>
    <div class="footer-fixed">
      <el-row justify="center">
        <el-button type="primary" size="small" @click="submit(ruleRefForm)">保存</el-button>
        <el-button size="small" @click="resetForm(ruleRefForm)">取消</el-button>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {toRefs, computed, ref, reactive} from 'vue'
import {createWorkHistory, editWorkHistory} from '/@/api/oa/staff/workHistory'

export default {
  name: 'WorkDetail',
  props: {
    dialogDrawer: {
      type: Boolean,
      default: false
    },
    workHistory: {
      type: Object,
      default: {}
    }
  },
  setup(props, context) {
    const {dialogDrawer} = toRefs(props)
    const drawer = computed({
      get: () => dialogDrawer.value,
      set: val => {
        context.emit('update:dialogDrawer', val)
      }
    })
    const {workHistory} = toRefs(props)
    const workHistoryData = computed({
      get: () => workHistory.value,
      set: val => {
        context.emit('update:workHistory', val)
      }
    })
    const ruleRefForm = ref()
    const state = reactive({
      formData: workHistoryData
    })
    const close = () => {
      context.emit('update:dialogDrawer', false)
    }
    const submit = async (formEl) => {
      if (!formEl) return
      await formEl.validate(async (valid) => {
        if (valid) {
          try {
            if (state.formData.id) {
              await editWorkHistory(state.formData)
            } else {
              await createWorkHistory(state.formData)
            }
            context.emit('update:dialogDrawer', false)
          } catch (e) {
            console.log(e)
          }
        }
      })
    }
    const resetForm = (formEl) => {
      if (!formEl) return
      context.emit('update:dialogDrawer', false)
      formEl.resetFields()
    }
    // const addWork = () => {
    //   state.formData.list.push({})
    // }
    // const delForm = index => {
    //   state.formData.list.splice(index, 1)
    // }
    return {
      drawer,
      workHistoryData,
      ruleRefForm,
      close,
      submit,
      resetForm,
      // addWork,
      // delForm,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped lang="scss">
.refForm {
  padding-bottom: 80px;
}

:deep(.el-form-item) {
  align-items: flex-start;
}

.footer-fixed {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  padding: 10px 0;
  background: #fff;
  z-index: 999;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;

  .add-fixed {
    padding-left: 20px;
    color: var(--el-color-primary);
    cursor: pointer;
    border-bottom: 1px dotted var(--el-border-color-light);
    padding-bottom: 10px;
    margin-bottom: 10px;

    .add-tips {
      margin-left: 5px;
      font-size: 13px;
    }
  }
}
</style>